#!/bin/bash

# 强制utf8mb4编码批量导入全国行政区划数据脚本
echo "开始utf8mb4批量导入全国行政区划数据..."

# 先清空表
echo "清空现有数据..."
docker exec mysql-5.7 mysql -uroot -p'abc#$123' smart_community -e "TRUNCATE TABLE sc_region;"

# 逐个导入省份数据，跳过TRUNCATE语句
for sql_file in AI_Task/区域管理测试数据补充_执行文档/province_sqls/province_*.sql; do
    province_name=$(basename "$sql_file" .sql | sed 's/province_[0-9]*_//')
    echo "正在导入: $province_name"
    # 复制文件到容器，跳过TRUNCATE语句
    docker exec mysql-5.7 bash -c "grep -v 'TRUNCATE TABLE' /tmp/province_sqls/$(basename $sql_file) > /tmp/temp_$(basename $sql_file)"
    # 强制utf8mb4编码导入
    docker exec -i mysql-5.7 mysql -uroot -p'abc#$123' --default-character-set=utf8mb4 smart_community < <(docker exec mysql-5.7 cat /tmp/temp_$(basename $sql_file))
    # 清理临时文件
    docker exec mysql-5.7 rm -f /tmp/temp_$(basename $sql_file)
done

# 显示导入结果统计
echo "导入完成，统计结果："
docker exec mysql-5.7 mysql -uroot -p'abc#$123' --default-character-set=utf8mb4 smart_community -e "
SELECT 
    region_type,
    COUNT(*) as count,
    CASE region_type 
        WHEN 1 THEN '省级'
        WHEN 2 THEN '市级' 
        WHEN 3 THEN '区县级'
        WHEN 4 THEN '街道级'
        WHEN 5 THEN '社区级'
        ELSE '其他'
    END as type_name
FROM sc_region 
GROUP BY region_type 
ORDER BY region_type;
"

echo "全国行政区划数据utf8mb4批量导入完成！" 